/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ${USER}
 * Date: ${YEAR}-${MONTH}-${DAY}
 * Time: ${TIME}
 */

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

class DouDiZhu extends JFrame {
    public JLabel player1Label;
     public JLabel player2Label;
     public JLabel player3Label;
     public JLabel leftoverLabel;
     public  JTextArea player1TextArea;
     public JTextArea player2TextArea;
     public JTextArea player3TextArea;
     public JTextArea leftoverTextArea;
     public JButton button1, button2, button3;
    public JPanel player1Panel;
     public JPanel player2Panel;
     public JPanel player3Panel;
     public JPanel remainPanel;
     public JPanel buttonPanel;
     public Font font;
    StringBuilder sb1 = new StringBuilder();
    StringBuilder sb2 = new StringBuilder();
    StringBuilder sb3 = new StringBuilder();
    List<String> poker= new ArrayList<>();
    //牌盒
    List<String> colors=new ArrayList<>();
    //花色
    List<String>numbers=new ArrayList<>();
    //数字
    public DouDiZhu() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置字体
        font = new Font("黑体", Font.PLAIN, 20);

        // 创建玩家1面板
        player1Panel = new JPanel();
        player1Panel.setLayout(new BorderLayout());
        player1Label = new JLabel("玩家A");
        player1Label.setFont(font);
        player1TextArea = new JTextArea(10, 25);
        player1TextArea.setBackground(Color.YELLOW);
        player1TextArea.setEditable(false);
        player1TextArea.setFont(font);
        JScrollPane player1ScrollPane = new JScrollPane(player1TextArea);
        player1Panel.add(player1Label, BorderLayout.NORTH);
        player1Panel.add(player1ScrollPane, BorderLayout.CENTER);

        // 创建玩家2面板
        player2Panel = new JPanel();
        player2Panel.setLayout(new BorderLayout());
        player2Label = new JLabel("玩家B");
        player2Label.setFont(font);
        player2TextArea = new JTextArea(10, 25);
        player2TextArea.setBackground(Color.YELLOW);
        player2TextArea.setEditable(false);
        player2TextArea.setFont(font);
        JScrollPane player2ScrollPane = new JScrollPane(player2TextArea);
        player2Panel.add(player2Label, BorderLayout.NORTH);
        player2Panel.add(player2ScrollPane, BorderLayout.CENTER);

        // 创建玩家3面板
        player3Panel = new JPanel();
        player3Panel.setLayout(new BorderLayout());
        player3Label = new JLabel("玩家C");
        player3Label.setFont(font);
        player3TextArea = new JTextArea(10, 25);
        player3TextArea.setBackground(Color.YELLOW);
        player3TextArea.setEditable(false);
        player3TextArea.setFont(font);
        JScrollPane player3ScrollPane = new JScrollPane(player3TextArea);
        player3Panel.add(player3Label, BorderLayout.NORTH);
        player3Panel.add(player3ScrollPane, BorderLayout.CENTER);
        //添加背景
        player1Panel.setBackground(Color.RED);
        player2Panel.setBackground(Color.RED);
        player3Panel.setBackground(Color.RED);

        // 创建底牌面板
        remainPanel = new JPanel();
        remainPanel.setLayout(new BorderLayout());
        leftoverLabel = new JLabel("底牌：");
        leftoverLabel.setFont(font);
        leftoverTextArea = new JTextArea(10, 25);
        leftoverTextArea.setEditable(false);
        leftoverTextArea.setFont(font);
        JScrollPane leftoverScrollPane = new JScrollPane(leftoverTextArea);
        remainPanel.add(leftoverLabel, BorderLayout.NORTH);
        remainPanel.add(leftoverScrollPane, BorderLayout.CENTER);
        remainPanel.setBackground(Color.RED);

        // 创建按钮
        buttonPanel = new JPanel();
        //按钮1：洗牌
        button1 = new JButton("洗牌");
        button1.setFont(font);
        button1.addActionListener(e -> {
            setCards();
            shuffleCards();
        });
        //按钮2：发牌
        button2 = new JButton("发牌");
        button2.setFont(font);
        button2.addActionListener(e -> cards());
        //按钮3：抢地主
        button3 = new JButton("抢地主");
        button3.setFont(font);
        button3.addActionListener(e -> rob());
        buttonPanel.add( button1);
        buttonPanel.add( button2);
        buttonPanel.add( button3);
        // 将玩家面板和底牌牌面板添加到主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(2, 2));
        mainPanel.add(player1Panel);
        mainPanel.add(player2Panel);

        mainPanel.add( remainPanel);
        mainPanel.add(player3Panel);
        // 按钮面板添加到主面板
        mainPanel.add(buttonPanel);
        // 将主面板添加到窗口
        add(mainPanel);
        setTitle("斗地主游戏");
        setSize(900, 900);
        setLocation(600,300);
        setVisible(true);
    }
    //1.创建牌
    public void setCards() {
        colors.add("红桃");
        colors.add("梅花");
        colors.add("方片");
        colors.add("黑桃");
        for(int i=2;i<=10;i++)
        {
            numbers.add(String.valueOf(i));
            //将i转为字符串
        }
        numbers.add("A");
        numbers.add("J");
        numbers.add("Q");
        numbers.add("K");
        //大、小王存储到poker牌盒中
        poker.add("大王");
        poker.add("小王");
        //循环嵌套遍历两个字符数组，组装52张牌
        for (String number : numbers) {
            for (String color : colors) {
                poker.add(color + number);
            }
        }
    }
    /*2.洗牌：使用集合的工具类Collections中的shuffle方法打乱牌的顺序*/
    public void shuffleCards(){
        Collections.shuffle(poker);
        // 将牌堆显示在剩余牌面板上
        StringBuilder sb = new StringBuilder();
        for (String card : poker) {
            sb.append(card).append("\n");
        }
        leftoverTextArea.setText(sb.toString());

        // 清空玩家手牌显示
        player1TextArea.setText("");
        player2TextArea.setText("");
        player3TextArea.setText("");
    }
    //3.发牌
    public void cards() {
        for (int i = 0; i < 51; i++) {
            if (i % 3 == 0) {
                sb1.append(poker.get(i)).append("\n");
                //player1：下标%3=0
            } else if (i % 3 == 1) {
                sb2.append(poker.get(i)).append("\n");
                //player2：下标%3=1
            } else {
                sb3.append(poker.get(i)).append("\n");
                //player3：下标%3=2
            }
        }
        // 显示玩家手牌

        player1TextArea.setText(sb1.toString());
        player2TextArea.setText(sb2.toString());
        player3TextArea.setText(sb3.toString());

        // 显示剩余牌
        leftoverTextArea.setText(poker.get(51) + "\n" + poker.get(52) + "\n" + poker.get(53));
    }
    //4.抢地主
    public void rob(){
        Random r=new Random();
        int m=  r.nextInt(33);
        if(m==1)
        {  sb1.append(poker.get(51)).append("\n");
            sb1.append(poker.get(52)).append("\n");
            sb1.append(poker.get(53)).append("\n");
            player1Label.setText("地主");
        }
        else if(m==2)
        {  sb2.append(poker.get(51)).append("\n");
            sb2.append(poker.get(52)).append("\n");
            sb2.append(poker.get(53)).append("\n");
            player2Label.setText("地主");}
        else
        { sb3.append(poker.get(51)).append("\n");
            sb3.append(poker.get(52)).append("\n");
            sb3.append(poker.get(53)).append("\n");
            player3Label.setText("地主");}

        player1TextArea.setText(sb1.toString());
        player2TextArea.setText(sb2.toString());
        player3TextArea.setText(sb3.toString());
        leftoverTextArea.setText("");
    }
    public static void main(String[] args) {
        new DouDiZhu();

    }
}
